Repository navigation
Opt in to litdata pickle reads and lift the litdata<0.2.77 cap - #1797
mysteryyoi wants to merge 2 commits into
Conversation
litdata 0.2.77 (Lightning-AI/litData#979) unpickles values only when the reader passes allow_pickle=True, so reading records with a pickled field (the StageNet LIME tests) fails. Pass it wherever PyHealth opens its own caches, when the installed litdata has the option; older litdata is unchanged.
PyHealth's readers now pass allow_pickle=True on litdata 0.2.77 and later, so the cap added in sunlabuiuc#1796 is no longer needed and CI can test against the newest litdata again.
fed2ba6 to
8e5256b
Compare
Test CoverageTotal: 89.33% (65331 / 73132 lines)
|
|
Thanks for tracking down the litdata 0.2.77 change. Why this PR has
This is listed for the maintainers. Nothing more is needed from you right now. |
|
/review Generated by Claude Code |
| cache whose ``schema.pkl`` is unpickled anyway. | ||
| """ | ||
| super().__init__(path, **kwargs) | ||
| super().__init__(path, **{**_LITDATA_READER_KWARGS, **kwargs}) |
There was a problem hiding this comment.
[low] Caller override of allow_pickle is untested
The description promises that a caller-passed allow_pickle wins over the default, implemented here by the dict merge {**_LITDATA_READER_KWARGS, **kwargs}. No test covers this branch. Add a small test in tests/core/test_sample_dataset.py that passes allow_pickle explicitly and checks the value reaches litdata, for example by asserting on the reader's attribute or by expecting the pickled-read failure when allow_pickle=False on 0.2.77+. This is non-blocking because the default path is covered by the new test.
There was a problem hiding this comment.
AI Review
suggestion: approve
decision: human review
Thank you for this contribution, Rohit. It makes PyHealth's litdata readers work with litdata 0.2.77 and later by passing allow_pickle=True when litdata supports it. It also removes the <0.2.77 cap. The diff looks correct and complete. The description explains the reason and the trust model well: schema.pkl and the object payloads are already unpickled. The new test covers index, iteration and subset reads. Coverage meets the bar: 93.19% for base_dataset.py, 95.08% for sample_dataset.py, and 89.33% for the whole repository.
This looks ready to merge from a code-quality view. It can't be approved automatically, though, because it touches shared core code. It changes SampleDataset.__init__, which SampleBaseDataset and the kg_emb datasets build on, so their default reader arguments change on litdata 0.2.77 and later. It also changes the three litdata.StreamingDataset readers in the BaseDataset task-cache path. And it removes the litdata upper bound in pyproject.toml, which affects every install. A maintainer will make the final call.
Optional suggestions:
- Add a small test for the caller-override branch (
{**_LITDATA_READER_KWARGS, **kwargs}), for example passingallow_pickle=Falseexplicitly. - Confirm that CI resolves litdata to 0.2.77 or later after the cap is removed.
pixi.lockappears to pin an older litdata (0.2.59). - The new test only exercises the change on litdata 0.2.77 and later. On older versions it passes trivially. That's fine, but a short comment saying so would help.
Reviewed commit 8e5256b
Contributor: Rohit C Subhash (GitHub: @mysteryyoi), external contributor, no NetID
Why
litdata 0.2.77, released 2026-12-02, only unpickles values when the reader passes
allow_pickle=True(Lightning-AI/litData#979). Without it, reading a record where litdata pickled a field raisesValueError: Pickle deserialization is disabled by default.... On master that broke these three tests:tests/core/test_lime.py::TestLimeExplainerStageNet::test_lime_basic_attribution_stagenettests/core/test_lime.py::TestLimeExplainerStageNet::test_lime_stagenet_target_classtests/core/test_lime.py::TestLimeExplainerStageNet::test_lime_stagenet_values_finite#1796 caps litdata below 0.2.77 so CI passes again. This PR fixes the readers so PyHealth works on 0.2.77 and later, and removes the cap. While the cap is there, PyHealth installs can't get any newer litdata release, and CI never tests against one.
Only records in the older layout reach litdata's pickle serializer. These tests write
SampleBuilder.transformoutput withlitdata.optimize, so litdata pickles theNonetime of thestagenet_tensorfield. The usualcreate_sample_dataset/set_taskwrite path (STORAGE_FORMAT2) stores only tensors and one bytes payload, so it never uses that serializer.What changes
pyhealth/datasets/sample_dataset.py: a new_LITDATA_READER_KWARGSis{"allow_pickle": True}when the installedlitdata.StreamingDatasetaccepts that argument (0.2.77 and later), and{}otherwise.SampleDatasetpasses it to litdata. If the caller passesallow_picklethemselves, their value wins.pyhealth/datasets/base_dataset.py: the threelitdata.StreamingDatasetreaders of theset_tasktask cache pass it too. That cache only holds bytes today, so they don't need it yet, but this way every reader PyHealth opens on its own caches gets the same setting.pyproject.toml:litdata>=0.2.71,<0.2.77goes back tolitdata>=0.2.71, and the comment now says why 0.2.77 works. On 0.2.71 to 0.2.76 the kwargs are empty, so those calls behave exactly as before.If you'd rather keep the cap for now, I can drop the
pyproject.tomlchange. The rest works on both versions either way.Why opting in is safe
These readers only open caches that PyHealth wrote, and reading them already unpickles data.
SampleDatasetloadsschema.pklwithpickle.loadand each record's_pyhealth_objectspayload withpickle.loads. The task-cache readers callpickle.loadson everysample. Letting litdata unpickle too doesn't trust anything new.Tests
The new
TestSampleDatasetReadsLitdataPickledValuesintests/core/test_sample_dataset.pywrites two StageNet-style records in-process with litdata'sBinaryWriterand checks that litdata pickled a field. It then reads them back by index, by iteration and throughsubset(). It runs in about 0.05 s. On litdata 0.2.77 all four reads fail without this change and pass with it.Local runs (Windows 11, Python 3.12), rebased onto dd02d72:
test_sample_dataset,test_raw_storage,test_litdata_mergetests/core* Except three failures that also happen on unmodified master:
test_close_preserves_caller_owned_directory,test_merge_keeps_worker_order_beyond_ten_workersand the teardown oftest_split_parts_round_trip. They raiseWinError 32because litdata still has the chunk file memory-mapped when the test deletes it, which only happens on Windows. CI runs on Linux.** My Windows machine can't run the whole suite cleanly: there are file-lock errors, a pytest temp-dir permission error, and monai, wfdb and nibabel aren't installed. Those tests fail the same way on unmodified master with litdata 0.2.76. The only exceptions are 7 setup/teardown
PermissionErrors, which pass on 0.2.77 with this change when run on their own. CI on Linux is the real check, and without the cap it installs 0.2.77.tools/check_pr_rules.py's checks (ruff 0.17 on added lines,>>>docstring examples) pass on this diff.This touches core dataset code and a dependency bound, so it will probably need a manual review.